package com.imay.live.adapter;

import android.text.SpannableStringBuilder;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.R;
import com.imay.live.activity.DiscoveryRankingGameListActivity;
import com.imay.live.activity.DiscoveryRankingListActivity;
import com.imay.live.activity.family.util.LevelUtil;
import com.imay.live.fragment.RankingFragment;
import com.imay.live.helper.LevelHelper;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.RankingModel;
import com.imay.live.model.UserModel;
import com.imay.live.utils.GlideUtil;
import com.imay.base.util.SpannableStringUtils;

import java.util.List;

/**
 * Created by  nick on 2017/4/20 0020.
 */

public class RankingAdapter extends BaseMultiItemQuickAdapter<RankingModel, BaseViewHolder> {


    public static final int ITEM_TYPE_FIRST = 1;
    public static final int ITEM_TYPE_SECOND = 2;
    public static final int ITEM_TYPE_NORMAL = 3;
    private String rankingType;
    private int type;

    public RankingAdapter(List<RankingModel> data, int type) {
        super(data);
        this.type = type;
        addItemType(ITEM_TYPE_FIRST, R.layout.item_ranking_top_one);
        addItemType(ITEM_TYPE_SECOND, R.layout.item_ranking_second);
        addItemType(ITEM_TYPE_NORMAL, R.layout.item_ranking_normal);

    }

    @Override
    protected void convert(BaseViewHolder helper, RankingModel item) {

        UserModel userModel = item.getUser();

        switch (item.getItemType()) {
            case ITEM_TYPE_FIRST:
                rankingType = item.getRankType();
                if (userModel != null) {
                    helper.setText(R.id.tv_name_ranking, userModel.getNick());
                    ImageView iv_hear_img_ranking = helper.getView(R.id.iv_hear_img_ranking);
                    GlideUtil.loadCircleBorderImage(iv_hear_img_ranking, userModel.getImgHead(), R.dimen.size_3dp, R.color.border_f3, R.mipmap.pnl_00_head108);
                }

                helper.setText(R.id.tv_value_ranking, getContent(item.getValue() + ""));

                break;
            case ITEM_TYPE_SECOND:
                int color = item.getRank() == 1 ? R.color.border_d4 : R.color.border_d8;
                ImageView iv_hear_img_ranking_second = helper.getView(R.id.iv_hear_img_ranking);
                GlideUtil.loadCircleBorderImage(iv_hear_img_ranking_second, item.getUser().getImgHead(), R.dimen.size_3dp, color, R.mipmap.pnl_00_head108);
                helper.setBackgroundRes(R.id.iv_rank, item.getRank() == 1 ? R.mipmap.dier : R.mipmap.disan);
                helper.setBackgroundRes(R.id.iv_ranking_bottom, item.getRank() == 1 ? R.mipmap.yajunjuntouxiangfuzhu : R.mipmap.jijunjuntouxiangfuzhu);
                helper.setBackgroundRes(R.id.iv_ranking_header, item.getRank() == 1 ? R.mipmap.yajunhuang : R.mipmap.jijunhuang);
                helper.setText(R.id.tv_value_ranking, item.getValue() + getContentByRankingType());
//
                break;
            case ITEM_TYPE_NORMAL:
                helper.setText(R.id.tv_ranking_rank, String.format(mContext.getString(R.string.ranking_rank), item.getRank() + 1));
                ImageView iv_hear_img_ranking_normal = helper.getView(R.id.iv_hear_img_ranking);
                GlideUtil.loadCircleImage(iv_hear_img_ranking_normal, item.getUser().getImgHead(), R.mipmap.pnl_00_head108);
                helper.setText(R.id.tv_value_ranking, item.getValue() + getContentByRankingType());
//
                break;
        }
        helper.setText(R.id.tv_name_ranking, userModel.getNick());
        helper.setText(R.id.tv_user_level_ranking, userModel.getUserLevel() + "");
        helper.setText(R.id.tv_anchor_level_ranking, userModel.getLiveLevel() + "");
        TextView tv_user_level_ranking = helper.getView(R.id.tv_user_level_ranking);
        TextView tv_anchor_level_ranking = helper.getView(R.id.tv_anchor_level_ranking);
        tv_user_level_ranking.setBackgroundResource(LevelHelper.getUserLevelResId(userModel.getUserLevel()));
        tv_anchor_level_ranking.setBackgroundResource(LevelHelper.getLiveLevelResId(userModel.getLiveLevel()));
//        helper.setChecked(R.id.cb_ranking_follow,userModel.isMyFollow());
        helper.setVisible(R.id.iv_ranking_follow, !userModel.isMyFollow());
        helper.setVisible(R.id.iv_row_ranking, userModel.isMyFollow());
        helper.addOnClickListener(R.id.iv_ranking_follow);
        helper.addOnClickListener(R.id.tv_name_ranking);
        helper.addOnClickListener(R.id.iv_hear_img_ranking);
        helper.setVisible(R.id.tv_anchor_level_ranking, type == RankingFragment.TYPE_DISCOVERY_RANKING || type == RankingFragment.TYPE_GAME_RANKING);
        helper.setVisible(R.id.tv_user_level_ranking, type != RankingFragment.TYPE_FAMILY);
        helper.setVisible(R.id.tv_anchor_family_ranking, type == RankingFragment.TYPE_FAMILY);
        helper.setTextColor(R.id.tv_anchor_family_ranking, mContext.getResources().getColor(LevelUtil.getLevelColor(userModel.getLv())));
        if (FamilyAccountManager.getInstance().getFamilyExps() != null
                && FamilyAccountManager.getInstance().getFamilyUserExps().containsKey(userModel.getLv())) {
            helper.setText(R.id.tv_anchor_family_ranking, FamilyAccountManager.getInstance().getFamilyUserExps().get(userModel.getLv()).getTitle());
        }
    }

    private SpannableStringBuilder getContent(String content) {
        SpannableStringBuilder builder = SpannableStringUtils.getBuilder("").
                append(content).setForegroundColor(mContext.getResources().getColor(R.color.text_30)).
                append(getContentByRankingType()).create();
        return builder;
    }


    private String getContentByRankingType() {
        if (type == RankingFragment.TYPE_FAMILY) {
            return mContext.getString(R.string.gongxian);
        }
        if (DiscoveryRankingListActivity.rankTypeArray[1].equals(rankingType) ||
                DiscoveryRankingGameListActivity.rankTypeArray[1].equals(rankingType)) {
            return mContext.getString(R.string.meizuan);
        } else if (DiscoveryRankingListActivity.rankTypeArray[2].equals(rankingType)) {
            return mContext.getString(R.string.ranking_like);
        } else if (DiscoveryRankingGameListActivity.rankTypeArray[0].equals(rankingType) ||
                DiscoveryRankingGameListActivity.rankTypeArray[2].equals(rankingType) ||
                DiscoveryRankingGameListActivity.rankTypeArray[3].equals(rankingType)) {
            return mContext.getString(R.string.fen);
        } else {
            return mContext.getString(R.string.meili);
        }
    }
}
